import Link from "next/link";
import { redirect } from "next/navigation";

import { DailyReportsView } from "@/components/reports/daily-reports-view";
import { getAuthenticatedUser, getDailyOutputs, getProjectSummaries } from "@/lib/server-data";

export default async function DailyReportsPage() {
  const user = await getAuthenticatedUser();

  if (!user) {
    redirect("/auth/login");
  }

  const today = new Date().toISOString().slice(0, 10);
  const [outputs, projects] = await Promise.all([
    getDailyOutputs({ from: today, to: today }),
    getProjectSummaries(),
  ]);

  return (
    <section className="mx-auto flex w-full max-w-5xl flex-col gap-6 px-6 py-12">
      <div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
        <div className="space-y-1">
          <h1 className="text-2xl font-semibold">日报记录</h1>
          <p className="text-sm text-muted-foreground">
            查看指定日期的产值上报记录，可导出 CSV 备份或进一步分析。
          </p>
        </div>
        <Link
          href="/outputs/new"
          className="inline-flex items-center rounded-md bg-primary px-3 py-2 text-xs font-medium text-primary-foreground transition hover:bg-primary/90"
        >
          新增日报
        </Link>
      </div>
      <DailyReportsView initialOutputs={outputs} initialDate={today} initialProjects={projects} />
    </section>
  );
}
